package com.yishusmt.mobsmt.serviceimpl;

import com.yishusmt.mobsmt.dto.base.Result;
import com.yishusmt.mobsmt.dto.request.AddressReq;
import com.yishusmt.mobsmt.dto.request.DefaultReq;
import com.yishusmt.mobsmt.dto.response.Tips;
import com.yishusmt.mobsmt.dto.response.UserRes;
import com.yishusmt.mobsmt.entity.ReceivingAddressEntity;
import com.yishusmt.mobsmt.enums.ResultCodeEnum;
import com.yishusmt.mobsmt.mapper.ReceivingAddressEntityMapper;
import com.yishusmt.mobsmt.mapper.ReceivingAddressInfoMapper;
import com.yishusmt.mobsmt.mapper.UserInfoMapper;
import com.yishusmt.mobsmt.service.AddressService;
import com.yishusmt.mobsmt.utils.DateUtil;
import com.yishusmt.mobsmt.utils.TipsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址服务实现
 *
 * @author zhangyi
 * @date 2018/8/13 10:51
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private ReceivingAddressEntityMapper receivingAddressEntityMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ReceivingAddressInfoMapper receivingAddressInfoMapper;


    @Override
    public Result add(AddressReq addressReq) {
        ReceivingAddressEntity receivingAddressEntity = new ReceivingAddressEntity();
        UserRes userRes = userInfoMapper.selectUserSelective(addressReq.getPhone());
        if (userRes != null) {
            receivingAddressEntity.setUserId(userRes.getId());
            receivingAddressEntity.setCity(addressReq.getCity());
            receivingAddressEntity.setCounty(addressReq.getCounty());
            receivingAddressEntity.setPostCode(addressReq.getPostCode());
            receivingAddressEntity.setDetailed(addressReq.getDetailed());
            receivingAddressEntity.setProvince(addressReq.getProvince());
            receivingAddressEntity.setReceivingName(addressReq.getReceivingName());
            receivingAddressEntity.setReceivingPhone(addressReq.getReceivingPhone());
            //添加时间
            receivingAddressEntity.setCreateTime(DateUtil.getCurrentDate());
            receivingAddressEntity.setUpdateTime(DateUtil.getCurrentDate());
            int i = receivingAddressEntityMapper.insertSelective(receivingAddressEntity);
            System.out.println("插入地址成功！");
            return 0 == i ? TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "添加地址失败")
                    : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS, "添加地址成功");
        } else {
            return TipsUtil.returnTips(ResultCodeEnum.UNKNOWN_ERROR, "该用户不存在");
        }
    }

    /**
     * 地址删除
     *
     * @param addressId
     * @return
     */
    @Override
    public Result del(Long addressId) {
        ReceivingAddressEntity receivingAddressEntity = new ReceivingAddressEntity();
        receivingAddressEntity.setId(addressId);
        receivingAddressEntity.setIsInvalid(1);
        int i = receivingAddressEntityMapper.updateByPrimaryKeySelective(receivingAddressEntity);
        return 0 == i ? TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "删除失败！")
                : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS, "删除成功！");
    }

    /**
     * 修改地址
     *
     * @param receivingAddressEntity
     * @return
     */
    @Override
    public Result update(ReceivingAddressEntity receivingAddressEntity) {

        receivingAddressEntity.setUpdateTime(DateUtil.getCurrentDate());
        int i = receivingAddressEntityMapper.updateByPrimaryKeySelective(receivingAddressEntity);
        if(receivingAddressEntity.getIsDefault() == 1){
            //mybatis修改默认地址信息
            DefaultReq defaultReq = new DefaultReq();
            defaultReq.setId(receivingAddressEntity.getId());
            defaultReq.setUserId(receivingAddressEntity.getUserId());
            receivingAddressInfoMapper.setAddressIsDefault(defaultReq);
        }
        return 0 == i ? TipsUtil.returnTips(ResultCodeEnum.OPERATION_FAIL, "修改失败！")
                : TipsUtil.returnTips(ResultCodeEnum.OPERATION_SUCCESS, "修改成功！");
    }

    /**
     * 查询所有地址
     *
     * @param phone
     * @return
     */
    @Override
    public Result selectAllAddress(String phone) {
        Result result = null;
        List<ReceivingAddressEntity> receivingAddressEntities = receivingAddressInfoMapper.selectAllAddress(phone);
        result = new Result(receivingAddressEntities);
        return result;
    }

    /**
     * 查看地址详细信息
     *
     * @param addressId
     * @return
     */
    @Override
    public Result selectAddressById(Long addressId) {
        ReceivingAddressEntity receivingAddressEntity = receivingAddressEntityMapper.selectByPrimaryKey(addressId);
        return new Result(receivingAddressEntity);
    }
}
